﻿using black_screen_client.Models;
using black_screen_client.Utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace black_screen_client
{
    /// <summary>
    /// Interaction logic for LogRecord.xaml
    /// </summary>
    public partial class LogRecord : Window
    {
        public ObservableCollection<LogRecordData> logs = new ObservableCollection<LogRecordData>();

        public LogRecord()
        {
            InitializeComponent();
            this.LogGrid.ItemsSource = logs;
        }


        public async void RefreshLog()
        {
            try
            {
                SqlHelper sqlhelper = new SqlHelper(ProjectTools.configDbConnString);
                string sql = "select top(10) [time],[type],[description] from log order by time desc ";
                DataTable result = sqlhelper.GetByTable(sql);
                if (result != null && result.Rows.Count > 0)
                {
                    logs.Clear();
                    foreach (DataRow row in result.Rows)
                    {
                        LogRecordData log = new LogRecordData();
                        log.Time = row["time"].ToString();
                        log.Description = row["description"].ToString();
                        log.Type = row["type"].ToString();
                        logs.Add(log);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Singleton.Info("数据库连接故障，刷新日志消息失败！请重新尝试！");
            }
        

        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.Visibility = Visibility.Collapsed;
        }
    }
}
